package com.zzyl.nursing.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;

import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.nursing.domain.NursingProjectPlan;
import com.zzyl.nursing.dto.NursingPlanDto;
import com.zzyl.nursing.mapper.NursingProjectPlanMapper;
import com.zzyl.nursing.vo.NursingPlanVo;
import com.zzyl.nursing.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.service.INursingPlanService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 护理计划Service业务层处理
 *
 * @author cxb
 * @date 2025-09-27
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id) {
        NursingPlan plan = getById(id);
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        BeanUtils.copyProperties(plan, nursingPlanVo);
        List<NursingProjectPlanVo> list = nursingProjectPlanMapper.selectByPlanId(id);
        if (list != null && !list.isEmpty()) {
            nursingPlanVo.setProjectPlans(list);
        }
        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划集合
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan) {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    @Transactional
    public int insertNursingPlan(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
        boolean save = save(nursingPlan);
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        if (projectPlans != null && !projectPlans.isEmpty()) {
            // 设置计划ID
            projectPlans.forEach(projectPlan -> {
                projectPlan.setPlanId(nursingPlan.getId());
            });
            // 批量插入
            nursingProjectPlanMapper.insertBatch(projectPlans);
        }
        return save ? 1 : 0;
    }

    /**
     * 修改护理计划
     *
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    @Transactional
    public int updateNursingPlan(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
        boolean update = updateById(nursingPlan);
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
            QueryWrapper<NursingProjectPlan> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("plan_id", nursingPlan.getId());
            nursingProjectPlanMapper.delete(queryWrapper);
            if (projectPlans != null && !projectPlans.isEmpty()) {
                // 批量设置计划ID
                projectPlans.forEach(projectPlan -> {
                    projectPlan.setPlanId(nursingPlan.getId());
                });
                // 批量插入
                nursingProjectPlanMapper.insertBatch(projectPlans);
            }

        return update ? 1 : 0;
    }

    /**
     * 批量删除护理计划
     *
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteNursingPlanById(Long id) {
        QueryWrapper<NursingProjectPlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("plan_id", id);
        nursingProjectPlanMapper.delete(queryWrapper);
        return removeById(id) ? 1 : 0;
    }

    @Override
    public List<NursingPlanVo> selectAllNursingPlan() {
        return nursingProjectPlanMapper.selectAllNursingPlan();
    }
}